import filterData from '@/common/filterData';
import department from '@/common/getDepartment';
const { department1, department2 } = department;
const regions = department1.slice(1);
const grids = department2.slice(1);
let gridStr = grids.flat().join(',')
const g_r_obj = grids.reduce((p, c, index) => {
	const region = regions[index];
	return {
		...p, ...c.reduce((p, c) => {
			p[c] = region;
			return p;
		}, {})
	};
}, {});
export function nlData(type, dataPool1, dataPool2, collectionOption) {

	let xp = {}, zt = {}, gd = {};
	grids.forEach((grid, index) => {
		let region = regions[index];
		grid.forEach((g) => {
			xp[g] = filterData.countDate(dataPool1[`${type}_xp`].all, [g]);
			zt[g] = filterData.countDate(dataPool1[`${type}_zt`].all, [g]);
			gd[g] = filterData.countDate(dataPool1[`${type}_gd`].all, [g]);
		})

	})
	const dates = NLObj[collectionOption.days];
	// var xp = regions.reduce((p, c) => {
	// 	p[c] = filterData.countDate(dataPool.dataPool1[`${type}_xp`].all, [c]);
	// 	return p;
	// }, {});
	// var zt = regions.reduce((p, c) => {
	// 	p[c] = filterData.countDate(dataPool.dataPool1[`${type}_zt`].all, [c]);
	// 	return p;
	// }, {});
	// var gd = regions.reduce((p, c) => {
	// 	p[c] = filterData.countDate(dataPool.dataPool1[`${type}_gd`].all, [c]);
	// 	return p;
	// }, {});
	let xpJGD = dataPool1[`${type}_xp`].all.filter(i => gridStr.indexOf(i.grid) < 0)
	let ztJGD = dataPool1[`${type}_zt`].all.filter(i => gridStr.indexOf(i.grid) < 0)
	let gdJGD = dataPool1[`${type}_gd`].all.filter(i => gridStr.indexOf(i.grid) < 0)

	var gongdan = dates.map((date) => {
		let dateData = grids.flat().map((grid) => {
			var x = xp[grid].find((d) => d.date === date) || { count: '' };
			var z = zt[grid].find((d) => d.date === date) || { count: '' };
			var g = gd[grid].find((d) => d.date === date) || { count: '' };
			return {
				date,
				region: g_r_obj[grid],
				xp: x.count,
				zt: z.count,
				gd: g.count,
				grid: grid,
			};
		});
		let d_xpjgd = xpJGD.filter(i => i.date === date)
		let d_gdjgd = gdJGD.filter(i => i.date === date)
		let d_ztjgd = ztJGD.filter(i => i.date === date)
		let xgzArr = regions.map(region => {
			let xpitem = d_xpjgd.filter(i => i.region === region).reduce((p, c) => { p.count += c.count; return p; }, { count: 0 })
			let gditem = d_gdjgd.filter(i => i.region === region).reduce((p, c) => { p.count += c.count; return p; }, { count: 0 })
			let ztitem = d_ztjgd.filter(i => i.region === region).reduce((p, c) => { p.count += c.count; return p; }, { count: 0 })
			return {
				date,
				region,
				grid: '其他',
				xp: xpitem ? xpitem.count : 0,
				gd: gditem ? gditem.count : 0,
				zt: ztitem ? ztitem.count : 0,
			}
		})
		let _dateData = dateData.concat(xgzArr)
		return _dateData;
	});

	var ztgdb = dataPool2[`${type}_zaituguidangbi`]
		.sort((a, b) => a['日期'] - b['日期'])
		.filter((a) => dates.includes(a['日期']));

	ztgdb.forEach((b, i) => {
		grids.flat().forEach((grid) => {
			let thatDay = gongdan.find((i) => i[0].date === b['日期']);
			if (thatDay) {
				let gridData = thatDay.find((i) => i.grid === grid);
				gridData && (gridData.ztgdb = b[grid]);
			}
		});
	});

	if (type === 'kuandai') {
		var hdl = dataPool2[`${type}_huidanlv`]
			.sort((a, b) => a['日期'] - b['日期'])
			.filter((a) => dates.includes(a['日期']));

		hdl.forEach((b, i) => {
			grids.flat().forEach((grid) => {
				let thatDay = gongdan.find((i) => i[0].date === b['日期']);
				if (thatDay) {
					let gridData = thatDay.find((i) => i.grid === grid);
					gridData && (gridData.hdl = b[grid]);
				}
			});
		});
	}

	const data = gongdan.flat();
	return data;
}

window.nlData = nlData;
